package com.example.springboot.service;

import com.example.springboot.dao.StaffMapper;
import com.example.springboot.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StaffServiceImp implements StaffService{
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public void insertStaff(Staff staff){
        staffMapper.insertStaff(staff);
    }

    @Override
    public List<Staff> selectAllStaff() {
        return staffMapper.selectAllStaff();
    }

    @Override
    public List<Staff> selectStaffByDept(Integer dept) {
        return staffMapper.selectStaffByDept(dept);
    }

    @Override
    public List<Staff> selectStaffByName(String name) {
        return staffMapper.selectStaffByName(name);
    }

    @Override
    public List<Staff> selectStaff(Integer id, String name, String gender, Integer dept) {
        return staffMapper.selectStaff(id, name, gender, dept);
    }

    @Override
    public void updateStaff(Staff staff) {
        Staff old = staffMapper.selectStaffById(staff.getId());
        if(staff.getDept()==null){
            staff.setDept(old.getDept());
        }
        if(staff.getGender()==null){
            staff.setGender(old.getGender());
        }
        staffMapper.updateStaff(staff);
    }

    @Override
    public Staff selectStaffById(Integer id) {
        return staffMapper.selectStaffById(id);
    }

    @Override
    public void deleteStaff(Integer id) {
        staffMapper.deleteStaff(id);
    }
}
